#pragma once
#include "Colorval.h"
#include "VideoSurfaceRegion.h"
#include "DirectDrawFwd.h"
#include "ImageFwd.h"

namespace ja2{ namespace sgp
{
	//! Video surface handle implementation.
	class VideoSurfaceHandle : boost::noncopyable
	{
		// Attributes
	private:
		//! Main surface.
		IDirectDrawSurfacePtr m_pSurfaceData;
		//! Hold all video memory Surfaces so that they my be restored.
		IDirectDrawSurfacePtr m_pSavedSurfaceData;
		//! Defaults to 0,0,0.
		COLORVAL TransparentColor;
		//! Clipper Surface.
		IDirectDrawClipperPtr m_pClipper;
		//! Is surface in video memory.
		bool m_IsInVideo;
		//! Width of surface.
		std::uint_least16_t m_Width;
		//! Height of surface.
		std::uint_least16_t m_Height;

		// Operations
	public:
		//! Get surface height.
		uint_least16_t height() const;
		//! Get surface width.
		uint_least16_t width() const;
		//! Is surface in video memory.
		bool is_in_video() const;
		//! Try to update backup if needed.
		void try_update_backup();
		//! Update backup surface.
		void update_backup();
		//! Fill surface with color.
		void fill(COLORVAL Color);
		//! Fill surface rect with color.
		void fill(COLORVAL Color, Rect const &Rect);
		//! Fast blit without color key.
		void blit_fast(VideoSurfaceHandle &SrcSurface, std::uint_least16_t DestX, std::uint_least16_t DestY, Rect const &SrcRect);
		//! Fast blit with source color key.
		void blit_fast_src_color_key(VideoSurfaceHandle &SrcSurface, std::uint_least16_t DestX, std::uint_least16_t DestY, Rect const &SrcRect);
		//! Fast blit with destination color key.
		void blit_fast_dest_color_key(VideoSurfaceHandle &SrcSurface, std::uint_least16_t DestX, std::uint_least16_t DestY, Rect const &SrcRect);
		//! Normal blit.
		void blit(VideoSurfaceHandle &SrcSurface, Rect const &SrcRect, Rect const &DestRect, std::uint_least32_t Flags);
		//! Blit with mirroring the Y.
		void blit_mirror_y(VideoSurfaceHandle &SrcSurface, Rect const &SrcRect, Rect const &DestRect, std::uint_least32_t Flags);
		//! Flip surface.
		void flip();
	private:
		//! Generic blit.
		void blit_generic(VideoSurfaceHandle &SrcSurface, Rect const &SrcRect, Rect const &DestRect, std::uint_least32_t Flags, LPDDBLTFX pFx);
		//! Generic fast blit.
		void blit_fast_generic(VideoSurfaceHandle &SrcSurface, std::uint_least16_t DestX, std::uint_least16_t DestY, Rect const &SrcRect, std::uint_least32_t Flags);

		// Construction/Destruction
	public:
		VideoSurfaceHandle(IDirectDrawSurfacePtr pSurfaceData, IDirectDrawSurfacePtr pSavedSurfaceData, COLORVAL TransparentColor, std::uint_least16_t Width, std::uint_least16_t Height);

		// Friends
		friend class VideoSurfaceLock;
	};

	typedef std::weak_ptr<VideoSurfaceHandle> VideoSurfaceHandleWeak_t;
} /*sgp*/} /*ja2*/
